package com.mz.ozy.activity.assets;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mz.ozy.R;
import com.mz.ozy.adapter.BannerPagerAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.TransferBean;
import com.mz.ozy.bean.TransferUserBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.TransferPresenter;
import com.mz.ozy.mvp.view.TransferContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.OptionsPickerView;
import com.mz.ozy.widget.ZoomPageTransformer;
import com.mz.ozy.widget.dialog.FitnessPlanDialog;
import com.mz.ozy.widget.dialog.PayPopupWindow;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 转账
 */
public class TransferActivity extends BaseActivity<TransferContract.View, TransferPresenter> implements TransferContract.View, ViewPager.OnPageChangeListener {


    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_usdt)
    TextView tvUsdt;
    @BindView(R.id.view_usdt)
    View viewUsdt;
    @BindView(R.id.tv_kt)
    TextView tvKt;
    @BindView(R.id.view_kt)
    View viewKt;
    @BindView(R.id.line_kt)
    LinearLayout lineKt;
    @BindView(R.id.tv_gqjf)
    TextView tvGqjf;
    @BindView(R.id.view_gqjf)
    View viewGqjf;
    @BindView(R.id.line_gqjf)
    LinearLayout lineGqjf;
    @BindView(R.id.line_usdt)
    LinearLayout lineUsdt;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    private String fee = "0";
    TransferBean mTransferBean;
    private boolean screen = false;
    private int paytype;
    private String coinType;

    private List<String> coinsList = new ArrayList<>();
    private OptionsPickerView pvOptions;
    private int options11;
    private List<TransferBean.BalancemapBean> urlList1 = new ArrayList<>();
    private BannerPagerAdapter mPagerAdapter;
    private String type = "0";
    private PayPopupWindow mPayPopupWindow;
    private String allType;
    private String max;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer);
        ButterKnife.bind(this);
        StatusBarUtil.setTranslucentForImageViewInFragment(TransferActivity.this, 20, null);

        screen = getIntent().getBooleanExtra("screen", false);
        paytype = getIntent().getIntExtra("paytype", 0);
        allType = getIntent().getStringExtra("type");
        final String address = SPUtils.getInstance(this).getString("address", "");
        if (!TextUtils.isEmpty(address)) {
            etAccount.setText(address);
            mPresenter.getUserName(TransferActivity.this, address);
        }
        mPresenter.getTransferInfo(this, type);
        etAmount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        etAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (Utils.editTextUtils(editable, etAmount)) {
                    if (TextUtils.isEmpty(editable.toString()) || editable.toString().equals(".") || Double.parseDouble(editable.toString()) == 0) {
                        tvFee.setText("0.0000");
                    } else {
                        tvFee.setText(Utils.decimalUtils(Double.parseDouble(editable.toString()) * Double.parseDouble(fee) + Double.parseDouble(max) + ""));

                    }
                }
            }
        });
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                final String account = etAccount.getText().toString().trim();
                if (!TextUtils.isEmpty(account) && account.length() == 7) {
                    mPresenter.getUserName(TransferActivity.this, account);
                } else {
                    mTvUserName.setText("");
                }
            }
        });

//        mPagerAdapter = new BannerPagerAdapter(urlList1, this);
        viewPager.setAdapter(mPagerAdapter);

        viewPager.setPageTransformer(true, new ZoomPageTransformer());
        viewPager.setOffscreenPageLimit(urlList1.size());//设置预加载的数量，这里设置了2,会预加载中心item左边两个Item和右边两个Item
        viewPager.addOnPageChangeListener(this);


    }

    @OnClick({R.id.scan_image, R.id.back, R.id.line_usdt, R.id.line_kt, R.id.line_gqjf, R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.scan_image:
                Intent intent1 = new Intent(this, ScanTranActivity.class);
                startActivityForResult(intent1, 105);

                break;
            case R.id.back:
                finish();
                break;
            case R.id.line_usdt:
                type = "0";
                etAmount.setText("");
                viewPager.setCurrentItem(Integer.parseInt(type));
                mPresenter.getTransferInfo(this, "0");
                break;
            case R.id.line_kt:
                type = "1";
                etAmount.setText("");
                viewPager.setCurrentItem(Integer.parseInt(type));
                mPresenter.getTransferInfo(this, "1");
                break;
            case R.id.line_gqjf:
                type = "2";
                etAmount.setText("");
                viewPager.setCurrentItem(Integer.parseInt(type));
                mPresenter.getTransferInfo(this, "2");
                break;
            case R.id.bt_confirm:
                if (TextUtils.isEmpty(etAccount.getText().toString().trim())) {
                    ToastUtils.showToast(getString(R.string.str_sk_username_hint));
                    return;
                }
                if (TextUtils.isEmpty(etAmount.getText().toString().trim())) {
                    ToastUtils.showToast(R.string.str_transfer_num_hint);
                    return;
                }
                if (Double.parseDouble(etAmount.getText().toString().trim()) < Double.parseDouble(mTransferBean.getMinamt())) {
                    ToastUtils.showToast(getString(R.string.slxz, mTransferBean.getMinamt()));
                    return;
                }

                mPayPopupWindow = new PayPopupWindow(this, new PayPopupWindow.OnClickListener() {
                    @Override
                    public void onConfirm(String str) {
                        mPayPopupWindow.dismiss();
                        mPresenter.obtainTransfer(TransferActivity.this, type, etAccount.getText().toString().trim(),
                                etAmount.getText().toString().trim(), str);

                    }
                });

                mPayPopupWindow.showAtLocation(this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);

                break;
        }
    }


    private FitnessPlanDialog mSelectDialog;

    private void showWallet() {
        if (mSelectDialog == null) {
            mSelectDialog = new FitnessPlanDialog(this, coinsList);
            mSelectDialog.setOnDialogListener(new FitnessPlanDialog.onDialogListener() {
                @Override
                public void onConfirm(int pos) {
                    options11 = pos;
                 /*   TransferBean.BalanceBean bean = coinListList.get(pos);
                    updateCoinsInfo(bean);*/
                    mSelectDialog.toggleDialog();
                }
            });
        }
        mSelectDialog.toggleDialog();
    }


    @Override
    public void getTransferInfoSuccess(TransferBean bean) {
        mTransferBean = bean;
        urlList1.clear();
        fee = bean.getFee();
        max = bean.getFixedfee();
        urlList1.addAll(bean.getBalancemap());
        mPagerAdapter.notifyDataSetChanged();
        if (allType.equals("5")) {
            allType = "0";
            viewPager.setCurrentItem(1);
        } else if (allType.equals("4")) {
            allType = "0";
            viewPager.setCurrentItem(2);
        }
        etAmount.setHint(R.string.str_transfer_num_hint);
    }

    @Override
    public void obtainTransferSuccess(JSONObject json) {
        ToastUtils.showToast(getString(R.string.str_transfer_success));
        EventBus.getDefault().postSticky(Constants.EventCode.REFRESH_DETAIL_LIST);
        finish();

    }

    @Override
    public void getUserNameSuccess(TransferUserBean bean) {
        final String username = bean.getUsername();
        mTvUserName.setText(username);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            type = "0";
            etAmount.setText("");
            viewUsdt.setBackgroundColor(getResources().getColor(R.color.white));
            viewKt.setBackgroundColor(0);
            viewGqjf.setBackgroundColor(0);

        } else if (position == 1) {
            type = "1";
            etAmount.setText("");
            viewUsdt.setBackgroundColor(0);
            viewKt.setBackgroundColor(getResources().getColor(R.color.white));
            viewGqjf.setBackgroundColor(0);
            mPresenter.getTransferInfo(this, "1");
        } else if (position == 2) {
            type = "2";
            etAmount.setText("");
            viewUsdt.setBackgroundColor(0);
            viewKt.setBackgroundColor(0);
            viewGqjf.setBackgroundColor(getResources().getColor(R.color.white));
            mPresenter.getTransferInfo(this, "2");
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == 105 && resultCode == RESULT_OK)) {
            String address = data.getStringExtra("userName");
            etAccount.setText(address);
            if (!TextUtils.isEmpty(address) && address.length() == 7) {
                mPresenter.getUserName(TransferActivity.this, address);
            }
        }

    }
}
